package com.ming.closeto.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ming.closeto.R;
import com.ming.closeto.base.BaseActivity;
import com.ming.closeto.manager.AppManager;

public class AppInfoActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_app_icon;
    private TextView tv_app_name;
    private TextView tv_app_packagename;
    private TextView tv_app_uninstall;
    private ListView mListView;

    private ArrayAdapter<String> mAdapter;

    private String appName;
    private String packageName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_info);
        initView();

    }

    private void initView() {
        iv_app_icon = (ImageView) findViewById(R.id.iv_app_icon);
        tv_app_name = (TextView) findViewById(R.id.tv_app_name);
        tv_app_packagename = (TextView) findViewById(R.id.tv_app_packagename);
        tv_app_uninstall = (TextView) findViewById(R.id.tv_app_uninstall);
        tv_app_uninstall.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.mListView);

        Intent intent = getIntent();
        appName = intent.getStringExtra("appName");
        packageName = intent.getStringExtra("packageName");

        if (!TextUtils.isEmpty(appName)) {
            getSupportActionBar().setTitle(appName);
            tv_app_name.setText(appName);
        } else {
            tv_app_name.setText("获取失败");
        }

        if (!TextUtils.isEmpty(packageName)) {
            tv_app_packagename.setText(packageName);
        } else {
            tv_app_packagename.setText("获取失败");
        }

        Drawable drawable = AppManager.getInstance(this).getAppIcon(packageName);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            iv_app_icon.setBackground(drawable);
        }

        String[] mStr = AppManager.getInstance(this).getAppPermissions(packageName);
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStr);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_app_uninstall:
                AppManager.getInstance(this).uninstanllApp(packageName);
                finish();
                break;
        }
    }
}
